
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

class NotificationRoute extends StatefulWidget{
  @override
  State<StatefulWidget> createState() {
    // TODO: implement createState
    return _NotificationRouteState();
  }

}

class _NotificationRouteState extends State<NotificationRoute> {
  var _msg = '';
  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return Scaffold(
      appBar: AppBar(
        title: Text("通知测试"),
      ),
      body: Center(
         child: NotificationListener<MyNotification>(
           onNotification: (notification) {
             setState(() {
               _msg+=notification.msg+"  ";
               print(_msg);
             });
             return true;
           },
           child: Column(
             mainAxisAlignment: MainAxisAlignment.center,
             children: <Widget>[
               Builder(
                 builder: (context) {
                   return FlatButton(
                     color: Colors.red,
                     child: Text("通知"),
                     onPressed: () {
                       MyNotification("test").dispatch(context);
                     },
                   );
                 },
               ),
               Text(_msg),
             ],
           ),
         ),
      ),
    );
  }
}
class MyNotification extends Notification{
  String msg;
  MyNotification(this.msg);
}